import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    // 登录
    {
      path: '/',
      name: 'login',
      component: () => import('../views/LoginReg/LoGin.vue')
    },

    //首页
    {
      path: '/index',
      name: 'index',
      component: () => import('../views/shouye/ShouYe.vue'),
      meta: { isAuth: true },
      children: [
        //用户管理
        {
          path: '',
          name: 'yonghu',
          component: () => import('../views/yonghu/YongHu.vue'),
          meta: { isAuth: true }
        },
        //轮播图管理
        {
          path: 'lunbo',
          name: 'lunbo',
          component: () => import('../views/syguanli/LunBo.vue'),
          meta: { isAuth: true }
        },
        //每日资讯管理
        {
          path: 'zixun',
          name: 'zixun',
          component: () => import('../views/syguanli/ZiXun.vue'),
          meta: { isAuth: true }
        },
        //宠物信息管理
        {
          path: 'pets',
          name: 'pets',
          component: () => import('../views/lingyang/PetsVue.vue'),
          meta: { isAuth: true }
        },
        //领养审核管理
        {
          path: 'shenhe',
          name: 'shenhe',
          component: () => import('../views/lingyang/ShenHe.vue'),
          meta: { isAuth: true }
        },
        //待领养管理
        {
          path: 'lingyang',
          name: 'lingyang',
          component: () => import('../views/lingyang/LingYang.vue'),
          meta: { isAuth: true }
        },
        //已领养管理
        {
          path: 'yiling',
          name: 'yiling',
          component: () => import('../views/lingyang/YiLing.vue'),
          meta: { isAuth: true }
        },
        //等待救助
        {
          path: 'daijiu',
          name: 'daijiu',
          component: () => import('../views/help/DaiJiu.vue'),
          meta: { isAuth: true }
        },
        //救助中
        {
          path: 'jiuzhong',
          name: 'jiuzhong',
          component: () => import('../views/help/JiuZhong.vue'),
          meta: { isAuth: true }
        },
        //救助完成
        {
          path: 'wanjiu',
          name: 'wanjiu',
          component: () => import('../views/help/WanJiu.vue'),
          meta: { isAuth: true }
        },
        //论坛管理
        {
          path: 'lunlist',
          name: 'lunlist',
          component: () => import('../views/luntan/LunLists.vue'),
          meta: { isAuth: true }
        },
        //评论管理
        {
          path: 'pinlun',
          name: 'pinlun',
          component: () => import('../views/luntan/PingLun.vue'),
          meta: { isAuth: true }
        },
        //小知识管理
        {
          path: 'zhishi',
          name: 'zhishi',
          component: () => import('../views/xiaozhishi/ZhiShi.vue'),
          meta: { isAuth: true }
        }
      ]
    },

    //404页面
    {
      path: '/NaN404',
      name: 'NaN404',
      component: () => import('../views/404/NanView.vue')
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查即将进入的路由是否匹配到了路由记录
  if (!to.matched.length) {
    // 如果没有匹配到任何路由记录，则重定向到登录页面
    next('/NaN404')
  } else {
    // 如果匹配到了路由记录，则正常跳转，判断跳转是否需要认证
    if (to.meta.isAuth) {
      //先判断用户是否登录
      if (localStorage.getItem('gly')) {
        next()
      } else {
        // 未登录跳转登录界面
        next({ name: 'login' })
      }
    } else {
      next()
    }
  }
})

export default router
